﻿package com.ostrich.project.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.HtmlUtil;
import com.base.web.BaseAction;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.service.impl.TostHrDepartinfoService;
import com.ostrich.project.bean.TostPjCostitem;
import com.ostrich.project.bean.TostPjPushmoney;
import com.ostrich.project.service.impl.TostPjPushmoneyService;
import com.ostrich.system.analysis.ParaminfoUtil;
import com.ostrich.system.bean.TostSysParaminfo;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostPjPushmoney")
public class TostPjPushmoneyController extends BaseAction {
	private final static Logger log = Logger
			.getLogger(TostPjPushmoneyController.class);

	// Servrice start
	@Autowired(required = false)// 自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjPushmoneyService<TostPjPushmoney> tostPjPushmoneyService;
	
	@Autowired(required = false)// 自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrDepartinfoService<TostHrDepartinfo> tosthrdepartinfo;

	private TostPjCostitem pjcost = null;

	/**
	 * ilook 首页
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(TostPjPushmoney tostPjPushmoney,
			HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> context = getRootMap();
		request.getSession().setAttribute("pjcos", tostPjPushmoney);
		Map<Integer, TostSysParaminfo> cmps = ParaminfoUtil.getParamByPid(70);
		Map<Integer, TostSysParaminfo> pushm = ParaminfoUtil.getParamByPid(103);
		Integer count = tostPjPushmoneyService.queryByCount(tostPjPushmoney);

		List<TostPjPushmoney> dataList1 = tostPjPushmoneyService
				.queryByList(tostPjPushmoney);
		// 判断页面是否直接跳转add的form表单的展示界面
		String flag = (count <= 0) ? "add" : "list";
		context.put("flag", flag);
		context.put("cptype", cmps);
		context.put("hdpId", pushm);
		context.put("pushmy", tostPjPushmoney);
		context.put("puss", dataList1);
		// 设置页面数据
		return forword("project/tabTostPjPushmoney", context);
	}

	/**
	 * json 列表页面
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/dataList")
	public void dataList(TostPjPushmoney tostPjPushmoney,
			HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		TostPjPushmoney tpjc = (TostPjPushmoney) request.getSession()
				.getAttribute("pjcos");
		tostPjPushmoney.setPjId(tpjc.getPjId());
		tostPjPushmoney.setPctId(tpjc.getPctId());

		List<TostPjPushmoney> dataList = tostPjPushmoneyService
				.queryByList(tostPjPushmoney);
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", tostPjPushmoney.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}

	/**
	 * 添加或修改数据
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public void save(TostPjPushmoney bean, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		TostPjPushmoney tpjc = (TostPjPushmoney) request.getSession()
				.getAttribute("pjcos");
		bean.setPjId(tpjc.getPjId());
		bean.setPctId(tpjc.getPctId());

		if (bean.getId() == null) {
			tostPjPushmoneyService.add(bean);
		} else {
			tostPjPushmoneyService.update(bean);
		}
		sendSuccessMessage(response, "保存成功~");
	}

	@RequestMapping("/getId")
	public void getId(Integer id, HttpServletResponse response)
			throws Exception {
		Map<String, Object> context = getRootMap();
		TostPjPushmoney bean = tostPjPushmoneyService.queryById(id);
		if (bean == null) {
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}

	@RequestMapping("/delete")
	public void delete(Integer[] id, HttpServletResponse response)
			throws Exception {
		tostPjPushmoneyService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}

	@RequestMapping("/getPjPush")
	public void getPjPush(HttpServletResponse response) {
		List<TostHrDepartinfo> dataList = tosthrdepartinfo.queryDapart();
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", (dataList.size() == 0) ? 0 : dataList.size());
		jsonMap.put("data", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
}
